package com.cskaoyan.service.impl;

import com.cskaoyan.bean.vo.GoodsSearchVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.model.*;
import com.cskaoyan.service.SearchHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class SearchHistoryServiceImpl implements SearchHistoryService {
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;
    @Autowired
    MarketKeywordMapper keywordMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public List<String> querySearchHistoryBykeyword(String keyword, int userId) {
        // 获取Keyword
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria1 = example.createCriteria().andDeletedEqualTo(false);
        if (keyword != null && !keyword.isEmpty()) {
            criteria1.andKeywordLike("%" + keyword + "%");
        }
        List<MarketKeyword> marketKeywords = keywordMapper.selectByExample(example);
        List<String> list = marketKeywords.stream().map(MarketKeyword::getKeyword).toList();
        // 获取模糊匹配的Goods
        MarketGoodsExample example1 = new MarketGoodsExample();
        example1.setOrderByClause("add_time desc");
        MarketGoodsExample.Criteria criteria = example1.createCriteria().andDeletedEqualTo(false);
        if (keyword != null && !keyword.isEmpty()) {
            criteria.andNameLike("%" + keyword + "%");
        }
        List<MarketGoods> goodsList = goodsMapper.selectByExample(example1);
        List<String> nameList = goodsList.stream().map(MarketGoods::getName).toList();
        // 搜索结合keyword和goods
        List<String> allList = Stream.concat(list.stream(), nameList.stream().limit(6))
                .collect(Collectors.toList());
        return allList;
    }

    @Override
    public void deleteSearchHistory(int userId) {

        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        example.createCriteria().andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<MarketSearchHistory> searchHistories = searchHistoryMapper.selectByExample(example);

        for (MarketSearchHistory searchHistory : searchHistories) {
            searchHistory.setUpdateTime(new Date());
            searchHistory.setDeleted(true);
            searchHistoryMapper.updateByPrimaryKeySelective(searchHistory);
        }
    }

    @Override
    public int insert(GoodsSearchVo goodsSearchVo, Integer userId) {
        MarketSearchHistory marketSearchHistory = MarketSearchHistory.builder()
                .userId(userId)
                .keyword(goodsSearchVo.getKeyword())
                .from("wx")
                .addTime(new Date())
                .updateTime(new Date())
                .deleted(false)
                .build();
        return searchHistoryMapper.insertSelective(marketSearchHistory);
    }
}
